package com.renwei.yunlong.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.WorkInformationActivity;
import com.renwei.yunlong.activity.work.WorkTrackMapActivity;
import com.renwei.yunlong.adapter.WorkHistoryPagerAdapter;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.HistoryBean;
import com.renwei.yunlong.event.ComplainPageRefreshEvent;
import com.renwei.yunlong.event.ProblemPageRefreshEvent;
import com.renwei.yunlong.event.SparePageRefreshEvent;
import com.renwei.yunlong.event.WorkPageRefreshEvent;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.HashMap;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;


/**
 * 工单  记录  页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

@SuppressLint("ValidFragment")
public class BaseHistoryFragment extends BaseLazyFragment implements HttpTaskListener {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private WorkHistoryPagerAdapter workHistoryAdapter;
    private String id;
    private String outsourceFlag = "0";
    private boolean isFirst;
    private String from;

    @SuppressLint("ValidFragment")
    public BaseHistoryFragment(String id, String from, String outsourceFlag) {
        this.id = id;
        this.from = from;
        this.outsourceFlag = outsourceFlag;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work_history_pager;
    }

    @Override
    public void onLazyLoad() {
        EventBus.getDefault().register(this);
        initView();
        initData();
    }

    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        workHistoryAdapter = new WorkHistoryPagerAdapter(getContext(),from);
        recyclerView.setAdapter(workHistoryAdapter);
        workHistoryAdapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                HistoryBean.Rows bean = workHistoryAdapter.getItem(position);
                if ("work".equals(from)) {
                    WorkTrackMapActivity.OpenActivity(getContext(), id, StringUtils.value(outsourceFlag),
                            bean.getOperationName());
                } else if ("spare".equals(from)) {
                    WorkInformationActivity.OpenActivity(getContext(), bean.getRequestId(), bean.getRequestId(), outsourceFlag);
                } else if ("problem".equals(from)) {

                } else if ("object".equals(from)) {
                    WorkInformationActivity.OpenActivity(getContext(),bean.getRequestId(),bean.getRequestId(),outsourceFlag);
                } else if ("complaint".equals(from)) {

                } else if ("know".equals(from)) {

                }
            }
        });
    }

    /**
     * 当前刷新
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(WorkPageRefreshEvent event) {
        isFirst = true;
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(ComplainPageRefreshEvent event) {
        isFirst = true;
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(SparePageRefreshEvent event) {
        isFirst = true;
        initData();
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefresh(ProblemPageRefreshEvent event) {
        isFirst = true;
        initData();
    }

    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("work".equals(from)) {
            map.put("model", "1");
        } else if ("spare".equals(from)) {
            map.put("model", "2");
        } else if ("problem".equals(from)) {
            map.put("model", "3");
        } else if ("object".equals(from)) {
            map.put("model", "4");
        } else if ("complaint".equals(from)) {
            map.put("model", "5");
        } else if ("know".equals(from)) {
            if ("1".equals(companyType)) {
                map.put("model", "6");
            } else if ("2".equals(companyType)) {
                map.put("model", "7");
            }
        }
        if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else if ("2".equals(companyType)) {
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("relatId", id);
        ServiceRequestManager.getManager().getWorkHistory(getContext(), outsourceFlag, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        HistoryBean bean = new Gson().fromJson(result, HistoryBean.class);
        if (bean.getMessage().getCode() == 200) {
            workHistoryAdapter.setData(bean.getRows());
            if (bean.getRows().size() == 0) {
                stateLayout.showEmptyView();
            } else {
                stateLayout.showContentView();
            }
            if (isFirst) {
                recyclerView.scrollToPosition(workHistoryAdapter.getItemCount() - 1);
            }
        }
    }

    @Override
    public void onException(int requestId, String code) {
        stateLayout.showErrorView();
    }
}
